$(function () {
    new AjaxUpload('#upload', {
        action: baseURL + 'ClassCourse2/upload?token=' + token,
        name: 'file',
        autoSubmit:true,
        responseType:"json",
        onSubmit:function(file, extension){
            if (!(extension && /^(xls|xlsx)$/.test(extension.toLowerCase()))){
                alert('只支持excel上传！');
                return false;
            }
        },
        onComplete : function(file, r){
            if(r.code == 0){
                alert("上传成功");
            }else{
                alert(r.msg);
            }
        }
    });

    $("#jqGrid").jqGrid({
        url: baseURL + 'CourseInfo/Select',
        datatype: "json",
        colModel: [
            { label: '课程代码', name: 'courseCode',index: "course_code" , width: 40,key: true },
             { label: '课程名称', name: 'courseName',index: "course_name" , width: 40 },
             { label: '学分', name: 'courseCredit',index: "course_credit" , width: 40 },
             { label: '周学时', name: 'courseWeekCycle',index: "course_week_cycle" , width: 40 },
             { label: '类型', name: 'courseType',index: "course_type" , width: 40 },
             { label: 's学时', name: 'courseSCycle',hidden:true , width: 40 },
             { label: 'l学时', name: 'courseLCycle',hidden:true, width: 40 },
             { label: '部门代码', name: 'courseCollegeCode',index: "course_college_code" , width: 40 },
             { label: '总学时', name: 'courseTotalCycle',index: "course_total_cycle" , width: 40 },
             { label: '级别', name: 'courseLevel',index: "course_level" , hidden:true , width: 40 },
             { label: '考察方式', name: 'courseAssessment',index: "course_assessment" ,hidden:true ,  width: 40 },
             { label: '备注', name: 'courseRemarks',index: "course_remarks" , width: 40 },
             { label: '性质', name: 'courseProperty', index: "course_property" ,width: 40 },
             { label: '部门名称', name: 'courseCollegeName',index: "course_college_name" , width: 40 },
             { label: '拼音简写', name: 'courseNamePinyin',index: "course_name_pinyin" , width: 40 },
             { label: '专业名称', name: 'courseProfession',index: "course_profession",hidden:true , width: 40 },
             { label: '操作', name: 'caozuo', width: 100, formatter: function(value, options, row){

                var r= '<a class="little-btn edit-btn" onclick="updateS(\''+value+'\')"><i class="fa fa-pencil-square-o"></i>&nbsp;修改</a>'+
                    '&nbsp;&nbsp;<a class="little-btn del-btn" onclick="delS(\' '+value+'\')"><i class="fa fa-trash-o"></i>&nbsp;删除</a>'

                return r;
                // '             <a class="btn btn-primary"><i class="fa fa-trash-o"></i>&nbsp;删除</a>';
            }}
         ],
        viewrecords: true,
        height: '100%',
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        // rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });
        },
        ondblClickRow:function(rowid){
            var rowData = $("#jqGrid").jqGrid('getRowData',rowid);
            var rowName =$("#jqGrid").jqGrid('getGridParam','colNames');
            var s="";
            var i=2;
            for(var key in rowData){
                s=s+rowName[i++]+':'+rowData[key]+'<br> ';
            }
            alert(s);
        }
    });
    $("#exportExcel").attr('href',baseURL+'CourseInfo/export?token='+token);
});
function updateS(id) {
    $.get(baseURL + "CourseInfo/Info/"+id, function(r){
        vm.showList = false;
        vm.title = "修改";
        vm.CourseInfo = r.CourseInfo;
        vm.courseCode = r.CourseInfo.courseCode;
    });
}

function delS(id) {
    confirm('确定要删除选中的记录？', function(){
        $.ajax({
            type: "POST",
            url: baseURL + "CourseInfo/Delete",
            contentType: "application/json",
            data: JSON.stringify(id),
            success: function(r){
                if(r.code == 0){
                    alert('操作成功', function(){
                        vm.reload();
                    });
                }else{
                    alert(r.msg);
                }
            }
        });
    });
}
var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null
        },
        showList: true,
        title: null,
        CourseInfo: {},
        courseCode: null
    },
    methods: {
        query: function () {
            vm.reload();
            $("#exportExcel").attr('href',baseURL+'StudentInfo/export?token='+token+"&searching="+vm.q.key);

        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.CourseInfo = {};
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            $.get(baseURL + "CourseInfo/Info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.CourseInfo = r.CourseInfo;
                vm.courseCode = r.CourseInfo.courseCode;
            });
        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "CourseInfo/Delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        pullin: function () {
            confirm('确定要开始数据导入？', function(){
                // $.ajax({
                //     type: "POST",
                //     url: baseURL + "Pullin/getClassCourseData",//ClassCourse2/upload
                //     success: function(r){
                //         if(r.code == 0){
                //             alert('操作成功', function(){
                //                 vm.reload();
                //             });
                //         }else{
                //             alert(r.msg);
                //         }
                //     }
                // });




            });
        },
        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            var url = vm.CourseInfo.courseCode == vm.courseCode ? "CourseInfo/Update" : "CourseInfo/Insert";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.CourseInfo),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{ 'courseName': vm.q.key},
                page:page
            }).trigger("reloadGrid");
        },
        validator: function () {
            if(isBlank(vm.CourseInfo.courseCode)){
                alert("课程代码不能为空");
                return true;
            }
            if(isBlank(vm.CourseInfo.courseName)){
                alert("课程名称不能为空");
                return true;
            }
            if(isBlank(vm.CourseInfo.courseCredit)){
                alert("学分不能为空");
                return true;
            }
            if(isBlank(vm.CourseInfo.courseWeekCycle)){
                alert("周学时不能为空");
                return true;
            }
            if(isBlank(vm.CourseInfo.courseType)){
                alert("类型不能为空");
                return true;
            }
            if(isBlank(vm.CourseInfo.courseSCycle)){
                alert("s学时不能为空");
                return true;
            }
            if(isBlank(vm.CourseInfo.courseLCycle)){
                alert("l学时不能为空");
                return true;
            }
            if(isBlank(vm.CourseInfo.courseCollegeCode)){
                alert("部门代码不能为空");
                return true;
            }
            if(isBlank(vm.CourseInfo.courseTotalCycle)){
                alert("总学时不能为空");
                return true;
            }
            if(isBlank(vm.CourseInfo.courseLevel)){
                alert("级别不能为空");
                return true;
            }
            if(isBlank(vm.CourseInfo.courseAssessment)){
                alert("考察方式不能为空");
                return true;
            }
            if(isBlank(vm.CourseInfo.courseRemarks)){
                alert("备注不能为空");
                return true;
            }
            if(isBlank(vm.CourseInfo.courseProperty)){
                alert("性质不能为空");
                return true;
            }
            if(isBlank(vm.CourseInfo.courseCollegeName)){
                alert("部门名称不能为空");
                return true;
            }
            if(isBlank(vm.CourseInfo.courseNamePinyin)){
                alert("拼音简写不能为空");
                return true;
            }
            if(isBlank(vm.CourseInfo.courseProfession)){
                alert("专业名称不能为空");
                return true;
            }
            }
        }
});